﻿using AutoMapper;
using Comm100.Application.Services;
using Comm100.Framework.Auditing;
using Comm100.Framework.Authorization;
using Comm100.Public.Audit;
using Bot.Application.Dto;
using Bot.Application.Services;
using Bot.Domain.Bo;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using Bot.Domain;
using System.Linq;
using System.IO;
using Microsoft.AspNetCore.Http;
using Comm100.Framework.Exceptions;

namespace Bot.Application.Images.Service
{
    public class ImageAppService : BaseAppService, IImageAppService
    {
        private IImageDomainService _domainService;

        public ImageAppService(IImageDomainService domainService) : base()
        {
            this._domainService = domainService;
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        public ImageDto Add(IFormFile file)
        {
            if (file.Length > 30 * 1024 * 1024) //30MB
            {
                throw new BusinessException(APIHelper.GetErrorMessageByCode(50050));
            }
            if (file.FileName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
                file.FileName.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) ||
                file.FileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
                file.FileName.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) ||
                file.FileName.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase))
            {                
                using (var stream = file.OpenReadStream())
                {
                    byte[] content = GetBytes(stream);
                    ImageCreateDto dto = new ImageCreateDto()
                    {
                        Name = file.FileName,
                        Content = content,
                    };
                    Image image = _domainService.Create(Mapper.Map<Image>(dto));
                    return Mapper.Map<ImageDto>(image);
                }
            }
            throw new BusinessException(APIHelper.GetErrorMessageByCode(50010));
        }

        public ImageCreateDto Get(Guid id)
        {
            Image image = _domainService.Get(id);
            if (image == null) throw new EntityNotFoundException(id, typeof(Image));
            return Mapper.Map<ImageCreateDto>(image);
        }

        private byte[] GetBytes(Stream stream)
        {
            using (MemoryStream mstream = new MemoryStream())
            {
                byte[] bytes = new byte[1024]; //此处不易设置太大或太小的值，且应该为2的次方
                if (stream.CanRead)
                {
                    while (true)
                    {
                        int length = stream.Read(bytes, 0, bytes.Length);
                        if (length <= 0)
                        {
                            break;
                        }
                        mstream.Write(bytes, 0, length);
                    }
                }
                return mstream.ToArray();
            }
        }
    }
}
